﻿// (c) Copyright 2013 Mike Riedel.
// This source is subject to the Microsoft Public License (Ms-PL).
// All other rights reserved.

using System;
using System.Threading.Tasks;

namespace ErrorHandlingBlock.Tests
{
   public class MockTarget
   {
      readonly Exception[] results;

      public MockTarget(params Exception[] results)
      {
         if (results == null)
            throw new ArgumentNullException("results");
         if (results.Length == 0)
            throw new ArgumentOutOfRangeException("results");

         this.results = results;
      }

      public Task<int> Action()
      {
         return Task.Run(
            () =>
            {
               var index = ActionCount;
               if (index >= results.Length)
                  index = results.Length - 1;

               ActionCount++;

               var result = results[index];

               if (result == null)
                  return ActionCount;

               throw result;
            });
      }

      public int ActionCount { get; private set; }
   }
}